package com.changpay.zywy.ui.activity.partent

import android.support.v4.app.FragmentTransaction
import android.view.View
import com.changpay.zywy.R
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.ui.fragment.qudao.QuDaoFragment
import com.changpay.zywy.utils.InputMethodUtils
import kotlinx.android.synthetic.main.activity_qu_dao_one.*

class QuDaoTwoActivity : BaseActivity() {


    private val TYPE_INDEX: String = "type_index"

    private val FRAGMENT_ONE = 0x01
    private val FRAGMENT_TWO = 0x02

    private var mIndex = FRAGMENT_TWO

    private var mOneFragment: QuDaoFragment? = null
    private var mTwoFragment: QuDaoFragment? = null


    private val onClickListener = View.OnClickListener { view ->
        when (view.id) {
            R.id.iv_back -> finish()
        }
    }

    override fun attachLayoutRes(): Int = R.layout.activity_qu_dao_one

    override fun initData() {
    }

    override fun initView() {
        iv_back.setOnClickListener(onClickListener)
        type_rg.setOnCheckedChangeListener { group, checkedId ->
            when (checkedId) {
                R.id.rb_one -> showFragment(FRAGMENT_ONE)
                R.id.rb_two -> showFragment(FRAGMENT_TWO)
            }
        }

        tv_search.setOnClickListener{
            val selectedTabPosition = mIndex
            val searchKey = et_search.text.toString().trim()
            InputMethodUtils.hideSoftInput(this)
            if (selectedTabPosition == FRAGMENT_ONE) {
                mOneFragment?.searchData(searchKey)
            } else if (selectedTabPosition == FRAGMENT_TWO) {
                mTwoFragment?.searchData(searchKey)
            }
        }

        showFragment(mIndex)
    }


    override fun start() {


    }


    /**
     * 隐藏所有的Fragment
     */
    private fun hideFragments(transaction: FragmentTransaction) {
        mOneFragment?.let { transaction.hide(it) }
        mTwoFragment?.let { transaction.hide(it) }
    }

    /**
     * 展示Fragment
     * @param index
     */
    private fun showFragment(index: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        hideFragments(transaction)
        mIndex = index
        when (index) {
            FRAGMENT_ONE //
            -> {
                if (mOneFragment == null) {
                    mOneFragment = QuDaoFragment.getInstance(0)
                    transaction.add(R.id.container, mOneFragment!!, "one")
                } else {
                    transaction.show(mOneFragment!!)
                }
            }
            FRAGMENT_TWO //
            -> {
                if (mTwoFragment == null) {
                    mTwoFragment = QuDaoFragment.getInstance(1)
                    transaction.add(R.id.container, mTwoFragment!!, "two")
                } else {
                    transaction.show(mTwoFragment!!)
                }
            }
        }
        transaction.commit()
    }

    fun changeTabTitle(position: Int, total: String) {
        if (rb_one != null) {
            var title = ""
            if (position == 0) {
                title = "商户($total)"
                rb_one.text = title
            } else if (position == 1) {
                title = "渠道($total)"
                rb_two.text = title
            }

        }
    }


}
